/*
 * @Description: 
 * @Author: yeeChen
 * @Date: 2022-12-16 16:55:45
 * @LastEditTime: 2022-12-16 17:10:45
 * @LastEditors: yeeChen
 */
// 导入对应的模块
const http = require('http')
const path = require('path')
const fs = require('fs')

// 创建启动服务器
const serve = http.createServer()
serve.on('request', (req, res) => {
    // 获取到客户端请求的url
    const url = req.url
    // /clock/index.html
    // /clock/index.css
    // /clock/index.js

    // 将请求到的url映射到具体文件的存放路径
    let fpath = ''
    if (url === '/') {
        fpath = path.join(__dirname,'./clock/index.html')
    } else {
        fpath = path.join(__dirname,'/clock',url)
    }

    // 根据映射过来的文件路径来读取文件内容
    fs.readFile(fpath, 'utf-8', (err, dataStr) => {
        if (err) {
            return res.end('<h1>404 Not found</h1>')
        } else {
            res.end(dataStr)
        }
    })

    //防止中文乱码
    // res.setHeader('Content-Type', 'text/html;charset=utf-8')
})
serve.listen(8080, () => {
    console.log('serve running at http://127.0.0.1:8080');
})